import RemoveList from '@/utils//RemoveList';

/** 移除保存信息 */
export interface RemoveSave<T> {
	remove: VoidFunction;
	value: T;
}

/** 保存项移除列表 */
export default class RemoveSaveList<T>{

	/** 列表 */
	private list: Set<RemoveSave<T>> = new Set();

	/** 移除列表 */
	private removeList: RemoveList = new RemoveList();

	/** 内容列表 */
	public get List(): RemoveSave<T>[] {
		return Array.from(this.list);
	}

	/**
	 * 新增
	 * @param value 要新增的内容
	 */
	public Add(value: T): RemoveSave<T> {
		const remove = this.removeList.Add(() => {
			this.list.delete(save);
		});
		const save: RemoveSave<T> = { value, remove };
		this.list.add(save);
		return save;
	}

	/** 移除 */
	public Remove(): void {
		this.removeList.Remove();
	}

	/** 清空 */
	public Clear(): void {
		this.removeList.Clear();
		this.list.clear();
	}

	/** 过滤 */
	public Filter(callbackfn: (value: RemoveSave<T>, index: number, array: RemoveSave<T>[]) => boolean) {
		this.list = new Set(Array.from(this.list).filter(callbackfn));
	}
}
